package ru.ifmo.cppsc.lexer;

import java.util.ArrayList;

/**
 * Table that holds identifiers
 *
 * @author Oleg Rybak
 */
public class IdTable {

    private ArrayList<Id> idList = new ArrayList<Id>();

    public Id get(int idx) {
        Id result;
        if (idx < 0 || idx >= idList.size()) {
            result = null;
        } else {
            result = idList.get(idx);
        }
        return result;
    }

    public int search(String value) {
        int result = -1;
        for (int i = 0; i < idList.size(); i++) {
            if (idList.get(i).getValue().equals(value)) {
                result = i;
                break;
            }
        }
        return result;
    }

    public int put(Id id) {
        int result = search(id.getValue());
        if (result == -1) {
            idList.add(id);
            result = idList.size() - 1;
        }
        return result;
    }
}
